let deptTree = [
  {
    id: "WSOslzzUg-OfyPGGVidqi",
    title: "擎创科技",
    children: [
      {
        id: "3E2X3qZA0db3lonc5h_Xs",
        title: "产品一部",
        children: [
          {
            id: "U2dPWLOxD-Gh7lqt4mw2Z",
            title: "一部测试组",
            children: [
              {
                id: "OcbCKlsNwa1yV4hV4kl2q",
                title: "测试一组",
                children: [
                  {
                    id: "QjNB3D_iZPA0UuPkeKKFx",
                    title: "王凯",
                  },
                  {
                    id: "eTcPMpVqHPqWeov1mOP5Q",
                    title: "王龙",
                  },
                ],
              },
            ],
          },
          {
            id: "OU3V1152OtmgWf4_pO8dT",
            title: "一部设计部",
            children: [
              {
                id: "BGE-fSUdl7WxYLCV4PwoV",
                title: "小李",
              },
              {
                id: "wPd-EoVwD9y96IMXEazne",
                title: "小张",
              },
              {
                id: "uimLbEVgrtHgq8B_UoLZj",
                title: "小王",
              },
            ],
          },
        ],
      },
      {
        id: "41ngVYWBCRpcrzPaaFyp3",
        title: "产品二部",
      },
    ],
  },
];
/**
 * @param {Array} data  部门树
 * @param {string} id   部门树中某个节点id
 * @returns {Array<string>}     所有的上层节点的id数组
 */
function findAllParentDepts(data, id) {
  let result = [];

  // 添加父级id的方法
  const addParentId = (arr1, pId = "-1") => {
    return arr1.map((item) => {
      if (item.children) {
        item.children = addParentId(item.children, item.id);
      }
      return {
        ...item,
        parentId: pId,
      };
    });
  };

  // 添加父级节点
  const arrTemp = addParentId(data);

  // 查找上层节点的数组方法
  const findParentIds = (arr2, id2, res = []) => {
    arr2.forEach((e) => {
      if (e.id === id2 && e.parentId !== "-1") {
        // 最外层不进行查找
        debugger
        res.unshift(e.parentId);
        findParentIds(arrTemp, e.parentId, res); // 从最后一级找到父级,继续传入全部的树数组          
      } else {
        if (e.children) {
          findParentIds(e.children, id2, res);
        }
      }
    });
    return res;
  };

  result = findParentIds(arrTemp, id);
  return result;
}

// 转为一维数组

console.log(
  "🚀  findAllParentDepts 测试结果:",
  findAllParentDepts(deptTree, "eTcPMpVqHPqWeov1mOP5Q")
);
